package com.qs.eggs.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.bean.ShoppingCart;
import com.qs.eggs.widgets.count.SnappingStepper;
import com.qs.eggs.widgets.count.SnappingStepperValueChangeListener;

import java.util.Iterator;
import java.util.List;

public class CartAdapter extends SimpleAdapter<ShoppingCart> implements BaseAdapter.OnItemClickListener {
    private CheckBox checkBox;
    private TextView textView;

   // private CartProvider cartProvider;


    public CartAdapter(Context context){
        super(context,R.layout.item_cart);
    };
    public CartAdapter(Context context, List<ShoppingCart> datas,final CheckBox checkBox,TextView tv) {
        super(context, R.layout.item_cart, datas);

        setCheckBox(checkBox);
        setTextView(tv);

     //   cartProvider = new CartProvider(context);

        setOnItemClickListener(this);

        showTotalPrice();

    }


    @Override
    protected void convert(BaseViewHolder viewHoder, final ShoppingCart item, final int position) {

        viewHoder.getTextView(R.id.tv_proname).setText(item.getProname());
        viewHoder.getTextView(R.id.tv_proprice).setText("￥"+item.getPrice());
        SnappingStepper snappingStepper=(SnappingStepper)viewHoder.getView(R.id.stepperCustom);
        SimpleDraweeView draweeView = (SimpleDraweeView) viewHoder.getView(R.id.propic);
        draweeView.setImageURI(Constants.URL_BASE+"productimages/"+item.getAutoname());
        CheckBox checkBox = (CheckBox) viewHoder.getView(R.id.checkbox);
        checkBox.setChecked(item.isChecked());
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShoppingCart cart =  getItem(position);
                cart.setIsChecked(!cart.isChecked());
                notifyItemChanged(position);

                checkListen();
                showTotalPrice();
            }
        });
        snappingStepper.setValue(item.getCount());
        snappingStepper.setOnValueChangeListener(new SnappingStepperValueChangeListener() {
            @Override
            public void onValueChange(View view, int value) {
                item.setCount(value);
                showTotalPrice();

            }
        });
    }

    public void showTotalPrice(){

        float total = getTotalPrice();

        textView.setText(Html.fromHtml("合计 ￥<span style='color:#eb4f38'>" + total + "</span>"), TextView.BufferType.SPANNABLE);
    }

    private  float getTotalPrice(){

        float sum=0;
        if(!isNull())
            return sum;

        for (ShoppingCart cart:
                datas) {
            if(cart.isChecked())
                sum += cart.getCount()*cart.getPrice();
        }

        return sum;
    }

    public boolean isNull(){

        return (datas !=null && datas.size()>0);
    }
    private void checkListen() {

        int count = 0;
        int checkNum = 0;
        if (datas != null) {
            count = datas.size();

            for (ShoppingCart cart : datas) {
                if (!cart.isChecked()) {
                    checkBox.setChecked(false);
                    break;
                } else {
                    checkNum = checkNum + 1;
                }
            }

            if (count == checkNum) {
                checkBox.setChecked(true);
            }

        }
    }
    public void setTextView(TextView textview){
        this.textView = textview;
    }

    public void setCheckBox(CheckBox ck){
        this.checkBox = ck;

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                checkAll_None(checkBox.isChecked());
                showTotalPrice();

            }
        });
    }

    public void delCart(){

        if(!isNull())
            return ;

        for(Iterator iterator = datas.iterator(); iterator.hasNext();){

            ShoppingCart cart = (ShoppingCart) iterator.next();
            if(cart.isChecked()){
                int position = datas.indexOf(cart);
                iterator.remove();
                notifyItemRemoved(position);
            }

        }

    }

    public void checkAll_None(boolean isChecked){


        if(!isNull())
            return ;


        int i=0;
        for (ShoppingCart cart :datas){
            cart.setIsChecked(isChecked);
            notifyItemChanged(i);
            i++;
        }


    }

    @Override
    public void onItemClick(View view, int position) {
        String as="";
        ShoppingCart cart =  getItem(position);
        cart.setIsChecked(!cart.isChecked());
        notifyItemChanged(position);

        checkListen();
        showTotalPrice();
    }
//    @Override
//    public void onItemClick(View view, int position) {
//        ShoppingCart cart =  getItem(position);
//        cart.setIsChecked(!cart.isChecked());
//        notifyItemChanged(position);
//
//        checkListen();
//        showTotalPrice();    }

}
